package com.boarsoft.boar.batch.demo.flow;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.boarsoft.boar.batch.demo.biz.DemoBatchBiz;
import com.boarsoft.boar.batch.demo.entity.Demo1;
import com.boarsoft.flow.core.bean.ProcessHandler;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

public class DemoFlowBatchProcess2 extends SimpleBizImpl implements ProcessHandler {
	@Autowired
	protected DemoBatchBiz biz;

	/**
	 * 并行向表中插入100万条记录
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Object process(String entry, Object data, Throwable throwable) throws Throwable {
		Map<String, Object> pm = (Map<String, Object>) data;
//		System.out.println("本线程号：" + Thread.currentThread().getId());
//		for (String m:pm.keySet()) {
//			System.out.println("process接收key: " + m + "  value: " + pm.get(m));
//		}
//		System.out.println("process接收map: " + pm);
		
		final int pageNo = (Integer) pm.get("pageNo");
		int start = pageNo * 2000 + 1;
		int end = (pageNo + 1) * 2000;
		List<Demo1> lt = new ArrayList<Demo1>();
		for (int i = start; i <= end; i++) {
			lt.add(new Demo1("a", "b"));
			if (i % 100 == 0) {
				biz.save(lt);
				lt.clear();
			}
		}
		if (!lt.isEmpty()) {
			biz.save(lt);
			lt.clear();
		}
		System.out.println("本线程complete: " + Thread.currentThread().getId());
		return data;
	}
}
